<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>xiaxuchen的博客</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="vue 组件 如果组件无法按照规范放在某些html标签中可以是用is属性将正常子标签转换为组件，如ul&gt;li：  12345678&lt;ul&gt;    &lt;!--    直接放置某组件无效，因为ul下面必须是li    &lt;组件名&gt;&lt;&#x2F;组件名&gt;    --&gt;    &lt;!--可以这样，渲染时会将li当做你的组件渲染--&gt;    &lt;li i">
<meta property="og:type" content="article">
<meta property="og:title" content="xiaxuchen的博客">
<meta property="og:url" content="http://yoursite.com/2020/08/07/%E5%89%8D%E7%AB%AF/vue%20%E7%BB%84%E4%BB%B6/index.html">
<meta property="og:site_name" content="xiaxuchen的博客">
<meta property="og:description" content="vue 组件 如果组件无法按照规范放在某些html标签中可以是用is属性将正常子标签转换为组件，如ul&gt;li：  12345678&lt;ul&gt;    &lt;!--    直接放置某组件无效，因为ul下面必须是li    &lt;组件名&gt;&lt;&#x2F;组件名&gt;    --&gt;    &lt;!--可以这样，渲染时会将li当做你的组件渲染--&gt;    &lt;li i">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2020-08-07T15:08:02.764Z">
<meta property="article:modified_time" content="2020-07-19T10:36:53.980Z">
<meta property="article:author" content="xiaxuchen">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="./atom.xml" title="xiaxuchen的博客" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  
<link rel="stylesheet" href="./css/style.css">

<meta name="generator" content="Hexo 5.0.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="./" id="logo">xiaxuchen的博客</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="./">Home</a>
        
          <a class="main-nav-link" href="./archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="./atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://yoursite.com"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-前端/vue 组件" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="./2020/08/07/%E5%89%8D%E7%AB%AF/vue%20%E7%BB%84%E4%BB%B6/" class="article-date">
  <time datetime="2020-08-07T15:08:02.764Z" itemprop="datePublished">2020-08-07</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
    <div class="article-entry" itemprop="articleBody">
      
        <h1 id="vue-组件"><a href="#vue-组件" class="headerlink" title="vue 组件"></a>vue 组件</h1><ol>
<li>如果组件无法按照规范放在某些html标签中可以是用is属性将正常子标签转换为组件，如ul&gt;li：</li>
</ol>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">ul</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--</span></span><br><span class="line"><span class="comment">    直接放置某组件无效，因为ul下面必须是li</span></span><br><span class="line"><span class="comment">    &lt;组件名&gt;&lt;/组件名&gt;</span></span><br><span class="line"><span class="comment">    --&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--可以这样，渲染时会将li当做你的组件渲染--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span> <span class="attr">is</span>=<span class="string">&quot;组件名&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>当然还有tr&gt;td,select&gt;option等</p>
<ol start="2">
<li><p>通过ref、$refs操作dom</p>
<p>当需要操作dom的时候，可以给想要操作的标签指定一个ref属性，如·<code>&lt;div ref=&#39;div&#39;&gt;&lt;/div&gt;</code>,则可以在vue中使用<strong>this.$refs.div</strong>操作</p>
</li>
<li><p>非父子组件传值(发布订阅模式)</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">Vue.prototype.bus = <span class="keyword">new</span> Vue()</span><br><span class="line">Vue.component(<span class="string">&#x27;bor1&#x27;</span>,&#123;</span><br><span class="line">    template:<span class="string">&#x27;&lt;div @click=&quot;handleClick&quot;&gt;hellp&lt;/div&gt;&#x27;</span>,</span><br><span class="line">    methods:&#123;</span><br><span class="line">        handleClick()&#123;</span><br><span class="line">            <span class="comment">//发布事件</span></span><br><span class="line">            <span class="built_in">this</span>.bus.$emit(<span class="string">&#x27;click&#x27;</span>,<span class="string">&#x27;hello&#x27;</span>)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br><span class="line">Vue.component(<span class="string">&#x27;bor2&#x27;</span>,&#123;</span><br><span class="line">    template:<span class="string">&#x27;&lt;div&gt;hellp&lt;/div&gt;&#x27;</span>,</span><br><span class="line">    mounted()&#123;</span><br><span class="line">        <span class="comment">//订阅</span></span><br><span class="line">        <span class="built_in">this</span>.bus.$on(<span class="string">&#x27;click&#x27;</span>,<span class="function">(<span class="params">data</span>)=&gt;</span>&#123;</span><br><span class="line">            <span class="built_in">console</span>.log(data)</span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<ol start="3">
<li><p>动态组件和v-once</p>
<p>可以通过动态组件将一个组件进行切换</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- html --&gt;</span><br><span class="line">&lt;component :is=<span class="string">&quot;type&quot;</span> &gt;&lt;/component&gt;</span><br><span class="line">&lt;button @click=<span class="string">&quot;handleClick&quot;</span>&gt;切换&lt;/button&gt;</span><br><span class="line">&lt;!-- js --&gt;</span><br><span class="line"><span class="keyword">const</span> vm = <span class="keyword">new</span> Vue(&#123;</span><br><span class="line">    data:&#123;</span><br><span class="line">        type:<span class="string">&#x27;el-col&#x27;</span></span><br><span class="line">    &#125;,</span><br><span class="line">    method:&#123;</span><br><span class="line">    <span class="comment">/*切换*/</span></span><br><span class="line">        handleClick()&#123;</span><br><span class="line">            <span class="built_in">this</span>.type = (<span class="built_in">this</span>.type === <span class="string">&#x27;el-button&#x27;</span>)? <span class="string">&#x27;el-col&#x27;</span> : <span class="string">&#x27;el-button&#x27;</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<p>在切换中组件会销毁，如果不想组件销毁可以在组件的模板中添加v-once属性<code>&lt;组件模板 v-once&gt;&lt;/组件模板&gt;</code>，组件切换时不会销毁，仍在内存中</p>
</li>
</ol>
</li>
</ol>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2020/08/07/%E5%89%8D%E7%AB%AF/vue%20%E7%BB%84%E4%BB%B6/" data-id="ckdkdagu8000cvout290te5ul" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="./2020/08/07/%E5%89%8D%E7%AB%AF/Vue%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          (no title)
        
      </div>
    </a>
  
  
    <a href="./2020/08/07/%E5%89%8D%E7%AB%AF/vue%20%E5%8A%A8%E7%94%BB/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title"></div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="./archives/2020/08/">August 2020</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="./2020/08/07/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B9%8B%E9%93%BE%E8%A1%A8/">(no title)</a>
          </li>
        
          <li>
            <a href="./2020/08/07/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B9%8B%E6%A0%88%E4%B8%8E%E9%98%9F%E5%88%97/">(no title)</a>
          </li>
        
          <li>
            <a href="./2020/08/07/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B9%8B%E6%95%B0%E7%BB%84/">(no title)</a>
          </li>
        
          <li>
            <a href="./2020/08/07/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/%E6%80%9D%E7%BB%B4%E5%AF%BC%E5%9B%BE/">(no title)</a>
          </li>
        
          <li>
            <a href="./2020/08/07/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/SpringBoot%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/">(no title)</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2020 xiaxuchen<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="./" class="mobile-nav-link">Home</a>
  
    <a href="./archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  
<link rel="stylesheet" href="./fancybox/jquery.fancybox.css">

  
<script src="./fancybox/jquery.fancybox.pack.js"></script>




<script src="./js/script.js"></script>




  </div>
</body>
</html>